//
//  MineOrderViewController.m
//  MJApp
//
//  Created by wangliang on 15/3/29.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineOrderViewController.h"
#import "MineOrderHeadCollectionViewCell.h"
#import "MineOrderContectCollectionViewCell.h"

@interface MineOrderViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *headCollectView;
@property (weak, nonatomic) IBOutlet UICollectionView *contectCollectView;
@property (nonatomic,strong)NSArray *headTitles;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineCenter;
@property (nonatomic,strong)MineOrderHeadCollectionViewCell *lastCell;
@property (nonatomic,strong)NSMutableArray *collectionList;

@end

@implementation MineOrderViewController

- (NSMutableArray *)collectionList {
    
    if (!_collectionList) {
        
        _collectionList = [NSMutableArray array];
    }
    return _collectionList;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self obationDate];
    [self Data];
    [self UI];
}

- (void)UI {
    
    [((TabBarController *)self.tabBarController) setTabbarHidden:YES];
    self.title = @"我的订单";
    self.automaticallyAdjustsScrollViewInsets = NO;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    self.headCollectView.tag = 1;
    self.headCollectView.delegate = self;
    self.headCollectView.dataSource = self;
    self.headCollectView.collectionViewLayout = flowLayout;
    [self.headCollectView registerNib:[UINib nibWithNibName:@"MineOrderHeadCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MineOrderHeadCollectionViewCell"];
    
    UICollectionViewFlowLayout *conFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    conFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    conFlowLayout.minimumLineSpacing = 0;
    self.contectCollectView.tag = 2;
    self.contectCollectView.delegate = self;
    self.contectCollectView.dataSource = self;
    self.contectCollectView.pagingEnabled = YES;
    self.contectCollectView.scrollEnabled = NO;
    self.contectCollectView.collectionViewLayout = conFlowLayout;
    [self.contectCollectView registerNib:[UINib nibWithNibName:@"MineOrderContectCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MineOrderContectCollectionViewCell"];
    
    [self resetIndex:0];
}

- (void)obationDate {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"user_id"] = [userInfoTool share].ID;
    dic[@"page"] = @"1";
    dic[@"pageSize"] = @"100";
    [WLHttpRequestAfter userGetOrderWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        if ([resultDictionary[@"status"] intValue] == 1) {
            
            [self.collectionList addObjectsFromArray:resultDictionary[@"data"]];
            [self.contectCollectView reloadData];
        }
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)resetIndex:(NSInteger)index {
    
    CGFloat wide = IOS_IPHONE_WINDOW_WIDE / self.headTitles.count;
    CGFloat constant = 0;
    if (index == 0) {
        
        constant = 2 * wide;
    }else if(index == 1) {
        
        constant = wide;
    }else if(index == 2){
        
        constant = 0;
    }else if(index == 3) {
        
        constant = -wide;
    }else if(index == 4) {
        
        constant = -2 * wide;
    }
    self.lineCenter.constant = constant;
}

- (void)Data {
    
    self.headTitles = @[@"全部",@"待付款",@"待发货",@"待收货",@"待评价"];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.headTitles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView.tag == 1) {
        
        MineOrderHeadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineOrderHeadCollectionViewCell" forIndexPath:indexPath];
        cell.infoDic = @{@"name":self.headTitles[indexPath.row]};
        if (indexPath.row == 0) {
            cell.selected = YES;
            self.lastCell = cell;
        }
        return cell;
    }else {
        
        MineOrderContectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineOrderContectCollectionViewCell" forIndexPath:indexPath];
        cell.infArr = self.collectionList;
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGSize size = CGSizeZero;
    if (collectionView.tag == 1) {
        size.height = 50;
        size.width = IOS_IPHONE_WINDOW_WIDE / self.headTitles.count;
    }else {
        
        size.width = IOS_IPHONE_WINDOW_WIDE;
        size.height = IOS_IPHONE_WINDOW_HIGH - CGRectGetHeight(self.headCollectView.frame);
    }
    
    
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView.tag == 1) {
        
        self.lastCell.selected = NO;
        [self resetIndex:indexPath.row];
        [self.contectCollectView setContentOffset:CGPointMake(IOS_IPHONE_WINDOW_WIDE * indexPath.row, 0) animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
